package sdc.com.petscalendar.fragments;

import java.util.ArrayList;
import java.util.Date;

import android.os.Bundle;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

import sdc.com.petscalendar.R;
import sdc.com.petscalendar.adapters.PetAdapter;
import sdc.com.petscalendar.objects.Pet;
import sdc.com.petscalendar.utils.Statics;

public class PetListFragment extends BaseFragment {
	private ArrayList<Pet> mDogs, mCats;
	private ListView mList;
	private PetAdapter mPetAdapter;
	private View mTabDog, mTabCat;
	private View mBtnAdd;
	private static boolean isTabDogSelected = true;
	private int mPosition = -2;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		addData();
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		if (mView == null) {
			mView = inflater.inflate(R.layout.fragment_petlist, container,
					false);

		}
		return mView;
	}

	@Override
	public void onViewCreated(View view, Bundle savedInstanceState) {
		super.onViewCreated(view, savedInstanceState);
		mList = (ListView) getView().findViewById(R.id.list);

		mTabDog = getView().findViewById(R.id.tab_dog);
		mTabCat = getView().findViewById(R.id.tab_cat);
		mTabDog.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				mTabDog.setSelected(true);
				mTabCat.setSelected(false);
				mPetAdapter = new PetAdapter(getActivity(), mDogs);
				mList.setAdapter(mPetAdapter);
				setTile(getResources().getString(R.string.pl_my_dogs));
				mPetAdapter.notifyDataSetChanged();
				initAdapter();
				isTabDogSelected = true;
			}
		});
		mTabCat.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				mTabDog.setSelected(false);
				mTabCat.setSelected(true);
				mPetAdapter = new PetAdapter(getActivity(), mCats);
				mList.setAdapter(mPetAdapter);
				setTile(getResources().getString(R.string.pl_my_cats));
				mPetAdapter.notifyDataSetChanged();
				initAdapter();
				isTabDogSelected = false;
			}
		});
		mBtnAdd = getView().findViewById(R.id.btn_add);
		mBtnAdd.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				mPosition = -1;
				Statics.sPet = new Pet();
				FragmentTransaction fragmentTransaction = getFragmentManager()
						.beginTransaction();
				fragmentTransaction.replace(R.id.frm_pet_list,
						new PetEditFragment());
				fragmentTransaction.addToBackStack(null);
				fragmentTransaction.commit();
			}
		});
		if (!isTabDogSelected)
			mTabCat.performClick();
		else
			mTabDog.performClick();
	}

	private void addData() {
		mDogs = new ArrayList<Pet>();
		for (int i = 1; i < 4; i++) {
			Pet pet = new Pet();
			pet.setName("Chien " + i);
			pet.setAvatar("");
			pet.setType(Pet.DOG);
			mDogs.add(pet);
		}

		mCats = new ArrayList<Pet>();
		for (int i = 1; i < 4; i++) {
			Pet pet = new Pet();
			pet.setName("Chat " + i);
			pet.setAvatar("");
			pet.setType(Pet.CAT);
			mCats.add(pet);
		}
	}

	@Override
	public void onResume() {
		super.onResume();
		if (mPosition >= 0) {
			if (Statics.sPet != null) {
				if (isTabDogSelected) {
					mDogs.set(mPosition, Statics.sPet);
					mPetAdapter = new PetAdapter(getActivity(), mDogs);
					initAdapter();
					mList.setAdapter(mPetAdapter);
				} else {
					mCats.set(mPosition, Statics.sPet);
					mPetAdapter = new PetAdapter(getActivity(), mCats);
					initAdapter();
					mList.setAdapter(mPetAdapter);
				}
			}
		} else if (mPosition == -1) {
			if (Statics.sPet != null) {
				mPetAdapter.add(Statics.sPet);
				mPetAdapter.notifyDataSetChanged();
			}
		}
		if (mTabDog.isSelected()) {
		}
		mPosition = -2;
	}

	private void initAdapter() {
		mPetAdapter.setItemClickListener(new PetAdapter.ItemClickListener() {

			@Override
			public void onItemClick(int position) {
				mPosition = position;
				Pet pet = mPetAdapter.getItem(position);
				pet.setId(1);
				pet.setWeight(20);
				pet.setRace("Dalmatien");
				pet.setSex(Pet.SEX_MALE);
				pet.setBirthday(new Date().getTime() - 86400000L * 2000);

				Statics.sPet = pet.clone();

				FragmentTransaction fragmentTransaction = getFragmentManager()
						.beginTransaction();
				fragmentTransaction.replace(R.id.frm_pet_list,
						new PetEditFragment());
				fragmentTransaction.addToBackStack(null);
				fragmentTransaction.commit();
			}
		});
		mPetAdapter
				.setItemCalendarClick(new PetAdapter.ItemCalendarClickListener() {

					@Override
					public void onItemCalendarClick(int position) {
						// TODO Auto-generated method stub

					}
				});

	}

	@Override
	public void onDestroy() {
		Statics.sPet = null;
		super.onDestroy();
	}
	

}
